﻿using System;
using System.Text;
using DevelopTool.Common;
using DevelopTool.Forms.BaseForms;
using Sunny.UI;

namespace DevelopTool.Forms.TextProcessForms
{
    /// <summary>
    /// 生成随机文本
    /// </summary>
    public partial class GenerateRandomTextForm : BaseUIPage
    {
        /// <summary>
        /// 特殊字符
        /// </summary>
        private readonly string symbolsStr = "!@＃$％^＆*()_+-={}[]|:;,.<>/?";

        public GenerateRandomTextForm()
        {
            InitializeComponent();
        }

        private void GenerateLipsumsTextForm_Load(object sender,EventArgs e)
        {
            this.cboGenerateType.SelectedIndex = 0;
            this.cboCase.SelectedIndex = 0;
        }

        /// <summary>
        /// 生成
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnGenerate_Click(object sender,EventArgs e)
        {
            string text = string.Empty;
            int length = this.upDownLength.Value;

            //生成类型
            switch(this.cboGenerateType.Text)
            {
                case "随机混合":
                    for(int i = 0 ; i < length ; i++)
                    {
                        string c = string.Empty;
                        int type = new Random(RandomEx.RandomSeed()).Next(1,5);
                        switch(type)
                        {
                            case 1: c = RandomEx.RandomPureChar(1); break;
                            case 2: c = RandomEx.RandomChars(1); break;
                            case 3: c = RandomEx.RandomChinese(1); break;
                            case 4: c = RandomEx.RandomNumber(1); break;
                        }
                        text += c;
                    }
                    break;
                case "字母":
                    text = RandomEx.RandomPureChar(length);
                    break;
                case "数字字母":
                    text = RandomEx.RandomChars(length);
                    break;
                case "中文":
                    text = RandomEx.RandomChinese(length);
                    break;
                case "数字":
                    text = RandomEx.RandomNumber(length);
                    break;
            }

            //大小写
            if(StringHelper.HasEnglish(text))
            {
                switch(this.cboCase.Text)
                {
                    case "随机":
                        string str = string.Empty;
                        foreach(char c in text)
                        {
                            bool upper = new Random(RandomEx.RandomSeed()).Next(0,2).ToBool();
                            str += upper ? c.ToString().ToUpper() : c.ToString().ToLower();
                        }
                        text = str;
                        break;
                    case "大写": text = text.ToUpper(); break;
                    case "小写": text = text.ToLower(); break;
                }
            }


            //包含特殊符号
            if(this.chbContainsSpecial.Checked)
            {
                if(text.Length > 1)
                {
                    char[] symbolsArr = symbolsStr.ToCharArray();//特殊符号数组

                    StringBuilder stringBuilder = new StringBuilder();
                    bool first = true;
                    foreach(char c in text)
                    {
                        //85%的几率插入特殊字符，并且要保证不能插入到第一个
                        if(new Random(RandomEx.RandomSeed()).Next(0,101) >= 85 && !first)
                        {                            
                            //取一个随机的特殊符号
                            stringBuilder.Append(symbolsArr[new Random(RandomEx.RandomSeed()).Next(0,symbolsStr.Length)]);
                        }
                        else
                        {
                            stringBuilder.Append(c);
                        }

                        first = false;
                    }
                    text = stringBuilder.ToString();
                }
            }

            this.codeEditorControl.ContentText = text;

            #region 字符串强度

            int strength = StringHelper.CheckStrength(text);//强度级别

            for(int i = 1 ; i <= 5 ; i++)
            {
                UIAvatar uIAvatar = this.pnlStrength.GetControl<UIAvatar>("strength" + i);
                if(strength >= i)
                {
                    uIAvatar.ForeColor = System.Drawing.Color.Orange;
                }
                else
                {
                    uIAvatar.ForeColor = System.Drawing.Color.Gray;
                }
            }
            #endregion 字符串强度
        }

    }
}
